<?php
/**
 *
 * Article(新闻模块)
 *
 */

class ContentAction extends AdminbaseAction {
	protected $dao, $fields;
	public function _initialize() {
		parent::_initialize ();
		$this->dao = D ( 'Admin.' . MODULE_NAME );
		
		$fields = F ( $this->moduleid . '_Field' );
		foreach ( $fields as $key => $res ) {
			$res ['setup'] = string2array ( $res ['setup'] );
			$this->fields [$key] = $res;
		}
		unset ( $fields );
		unset ( $res );
		$this->assign ( 'fields', $this->fields );
	}
	
	/**
	 * 列表
	 *
	 */
	public function index() {
		$template = file_exists ( TEMPLATE_PATH . '/' . GROUP_NAME . '/' . MODULE_NAME . '_index.html' ) ? MODULE_NAME . '_index' : 'Content_index';
		$this->_list ( MODULE_NAME );
		$this->display ( $template );
	}
	
	public function add() {
		$form = new Form ();
		$this->assign ( 'form', $form );
		$template = file_exists ( TEMPLATE_PATH . '/' . GROUP_NAME . '/' . MODULE_NAME . '_edit.html' ) ? MODULE_NAME . '_edit' : 'Content_edit';
		$this->display ( $template );
	}
	
	public function edit() {
		
		$id = $_REQUEST ['id'];
		
		if (MODULE_NAME == 'Page') {
			$Page = D ( 'Page' );
			$p = $Page->find ( $id );
			if (empty ( $p )) {
				$data ['id'] = $id;
				$data ['title'] = $this->categorys [$id] ['catname'];
				$data ['keywords'] = $this->categorys [$id] ['keywords'];
				
				$Page->add ( $data );
			}
		}
		$vo = $this->dao->getById ( $id );
		$form = new Form ( $vo );
		$this->assign ( 'vo', $vo );
		$this->assign ( 'form', $form );
		$template = file_exists ( TEMPLATE_PATH . '/' . GROUP_NAME . '/' . MODULE_NAME . '_edit.html' ) ? MODULE_NAME . '_edit' : 'Content_edit';
		$this->display ( $template );
	}
	
	/**
	 * 录入
	 *
	 */
	public function insert($module = '', $fields = array(), $userid = 0, $username = '', $groupid = 0) {
		$model = $module ? M ( $module ) : $this->dao;
		$fields = $fields ? $fields : $this->fields;
		$_POST = checkfield ( $fields, $_POST );
		if (empty ( $_POST ))
			$this->error ( L ( 'do_empty' ) );
		
		$_POST ['createtime'] = time ();
		$_POST ['updatetime'] = $_POST ['createtime'];
		$_POST ['userid'] = $userid ? $userid : $_SESSION ['userid'];
		$_POST ['username'] = $username ? $username : $_SESSION ['username'];
		if ($_POST ['style_color'])
			$_POST ['style_color'] = 'color:' . $_POST ['style_color'];
		if ($_POST ['style_bold'])
			$_POST ['style_bold'] = ';font-weight:' . $_POST ['style_bold'];
		if ($_POST ['style_color'] || $_POST ['style_bold'])
			$_POST ['title_style'] = $_POST ['style_color'] . $_POST ['style_bold'];
		
		$module = $module ? $module : MODULE_NAME;
		if (GROUP_NAME == 'User')
			$_POST ['status'] = $this->Role [$groupid] ['allowpostverify'] ? 1 : 0;
		
		if (false === $model->create ()) {
			$this->error ( $model->getError () );
		}
		$_POST ['id'] = $id = $model->add ();
		
		if ($id !== false) {
			$catid = $module == 'Page' ? $id : $_POST ['catid'];
			
			if ($_POST ['aid']) {
				$Attachment = M ( 'Attachment' );
				$aids = implode ( ',', $_POST ['aid'] );
				$data ['id'] = $id;
				$data ['catid'] = $catid;
				$data ['status'] = '1';
				$Attachment->where ( "aid in (" . $aids . ")" )->save ( $data );
			}
			
			$data = '';
			$cat = $this->categorys [$catid];
			$url = geturl ( $cat, $_POST, $this->Urlrule );
			$data ['id'] = $id;
			$data ['url'] = $url [0];
			$model->save ( $data );
			
			if ($cat ['presentpoint']) {
				$user = M ( 'User' );
				if ($cat ['presentpoint'] > 0)
					$user->setInc ( 'point', "id=" . $_POST ['userid'], $cat ['presentpoint'] );
				if ($cat ['presentpoint'] < 0)
					$user->setDec ( 'point', "id=" . $_POST ['username'], $cat ['presentpoint'] );
			}
			
			if ($cat ['ishtml'] && $_POST ['status']) {
				if ($module != 'Page' && $_POST ['status'])
					$this->create_show ( $id, $module );
				if ($this->sysConfig ['HOME_ISHTML'])
					$this->create_index ();
				$arrparentid = array_filter ( explode ( ',', $cat ['arrparentid'] . ',' . $cat ['id'] ) );
				foreach ( $arrparentid as $catid ) {
					if ($this->categorys [$catid] ['ishtml'])
						$this->clisthtml ( $catid );
				}
			}
			if (GROUP_NAME == 'Admin') {
				$this->assign ( 'jumpUrl', U ( $module . '/index' ) );
			} elseif (GROUP_NAME == 'User') {
				
				$this->assign ( 'jumpUrl', $_SERVER ['HTTP_REFERER'] );
			
		//$this->assign ( 'jumpUrl', U(GROUP_NAME.'-'.MODULE_NAME.'/add?moduleid='.$cat['moduleid']) );
			}
			
			$this->success ( L ( 'add_ok' ) );
		} else {
			$this->error ( L ( 'add_error' ) . ': ' . $model->getDbError () );
		}
	
	}
	
	function update($module = '', $fields = array(), $userid = 0, $username = '') {
		$model = $module ? M ( $module ) : $this->dao;
		$fields = $fields ? $fields : $this->fields;
		$_POST = checkfield ( $fields, $_POST );
		if (empty ( $_POST ))
			$this->error ( L ( 'do_empty' ) );
		
		$_POST ['updatetime'] = time ();
		if ($_POST ['style_color'])
			$_POST ['style_color'] = 'color:' . $_POST ['style_color'];
		if ($_POST ['style_bold'])
			$_POST ['style_bold'] = ';font-weight:' . $_POST ['style_bold'];
		if ($_POST ['style_color'] || $_POST ['style_bold'])
			$_POST ['title_style'] = $_POST ['style_color'] . $_POST ['style_bold'];
		
		$cat = $this->categorys [$_POST ['catid']];
		$module = $module ? $module : MODULE_NAME;
		$_POST ['url'] = geturl ( $cat, $_POST, $this->Urlrule );
		$_POST ['url'] = $_POST ['url'] [0];
		
		if (false === $model->create ()) {
			$this->error ( $model->getError () );
		}
		// 更新数据
		$list = $model->save ();
		if (false !== $list) {
			$id = $_POST ['id'];
			$catid = $module == 'Page' ? $id : $_POST ['catid'];
			
			if ($_POST ['aid']) {
				$Attachment = M ( 'Attachment' );
				$aids = implode ( ',', $_POST ['aid'] );
				$data ['id'] = $id;
				$data ['catid'] = $catid;
				$data ['status'] = '1';
				$Attachment->where ( "aid in (" . $aids . ")" )->save ( $data );
			}
			$cat = $this->categorys [$catid];
			if ($cat ['ishtml']) {
				if ($module != 'Page' && $_POST ['status'])
					$this->create_show ( $_POST ['id'], $module );
				if ($this->sysConfig ['HOME_ISHTML'])
					$this->create_index ();
				$arrparentid = array_filter ( explode ( ',', $cat ['arrparentid'] . ',' . $cat ['id'] ) );
				foreach ( $arrparentid as $catid ) {
					if ($this->categorys [$catid] ['ishtml'])
						$this->clisthtml ( $catid );
				}
			}
			$this->assign ( 'jumpUrl', $_SERVER ['HTTP_REFERER'] );
			$this->success ( L ( 'edit_ok' ) );
		} else {
			//错误提示
			$this->success ( L ( 'edit_error' ) . ': ' . $model->getDbError () );
		}
	}
	
	function statusallok() {
		
		$module = MODULE_NAME;
		$model = M ( $module );
		$ids = $_POST ['ids'];
		if (! empty ( $ids ) && is_array ( $ids )) {
			$id = implode ( ',', $ids );
			$data = $model->select ( $id );
			if ($data) {
				foreach ( $data as $key => $r ) {
					$model->save ( array (id => $r ['id'], status => 1 ) );
					if ($this->categorys [$r ['catid']] ['ishtml'] && $r ['status'])
						$this->create_show ( $r ['id'], $module );
				}
				$cat = $this->categorys [$r ['catid']];
				if ($cat ['ishtml']) {
					if ($this->sysConfig ['HOME_ISHTML'])
						$this->create_index ();
					$arrparentid = array_filter ( explode ( ',', $cat ['arrparentid'] . ',' . $cat ['id'] ) );
					foreach ( $arrparentid as $catid ) {
						if ($this->categorys [$catid] ['ishtml'])
							$this->clisthtml ( $catid );
					}
				}
				$this->success ( L ( 'do_ok' ) );
			} else {
				$this->error ( L ( 'do_error' ) . ': ' . $model->getDbError () );
			}
		} else {
			$this->error ( L ( 'do_empty' ) );
		}
	}
	
	/*状态*/
	
	public function status() {
		$module = MODULE_NAME;
		$model = D ( $module );
		if ($model->save ( $_GET )) {
			$_POST = '';
			$_POST = $model->find ( $_GET ['id'] );
			$cat = $this->categorys [$_POST ['catid']];
			if ($cat ['ishtml']) {
				if ($module != 'Page' && $_POST ['status'])
					$this->create_show ( $_POST ['id'], $module );
				if ($this->sysConfig ['HOME_ISHTML'])
					$this->create_index ();
				$arrparentid = array_filter ( explode ( ',', $cat ['arrparentid'] . ',' . $cat ['id'] ) );
				foreach ( $arrparentid as $catid ) {
					if ($this->categorys [$catid] ['ishtml'])
						$this->clisthtml ( $catid );
				}
			}
			
			$this->success ( L ( 'do_ok' ) );
		} else {
			$this->error ( L ( 'do_error' ) );
		}
	}

}
